package com.qipay.mapper;

import com.qipay.baas.model.Cp;
import com.qipay.baas.model.CpAccount;
import com.qipay.baas.model.CpZhiding;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface CpMapper extends com.qipay.baas.mapper.CpMapper {

    @SelectProvider(type = CpSqlProvider.class, method = "allCpList")
    @Results({
            @Result(column = "account_state", property = "accountState", jdbcType = JdbcType.BIGINT),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "agent_id", property = "agentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "total", property = "total", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.BIGINT),
            @Result(column = "widthdraw_amount", property = "widthdrawAmount", jdbcType = JdbcType.BIGINT),
            @Result(column = "balance_disabled", property = "balanceDisabled", jdbcType = JdbcType.BIGINT)
    })
    List<CpAccount> allCpList(String name,Long cpId,String person,String phone,Byte type,Long agentId);

    @SelectProvider(type = CpSqlProvider.class, method = "allAgentList")
    @Results({
            @Result(column = "withdraw_fee", property = "withdrawFee", jdbcType = JdbcType.BIGINT),
            @Result(column = "agent_fee", property = "agentFee", jdbcType = JdbcType.BIGINT),
            @Result(column = "account_state", property = "accountState", jdbcType = JdbcType.BIGINT),
            @Result(column = "cp_id", property = "cpId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "agent_id", property = "agentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "total", property = "total", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.BIGINT),
            @Result(column = "widthdraw_amount", property = "widthdrawAmount", jdbcType = JdbcType.BIGINT),
            @Result(column = "balance_disabled", property = "balanceDisabled", jdbcType = JdbcType.BIGINT)
    })
    List<CpAccount> allAgentList(String name,Long cpId,String person,String phone,Byte type,Long agentId);


    @Select("SELECT IFNULL(SUM(t3.total),0) + IFNULL(SUM(t5.disTotal),0)total,IFNULL(SUM(t3.fee),0)fee,IFNULL(SUM(t4.drawAmount),0)widthdraw_amount,IFNULL(sum(t5.disTotal),0)balance_disabled from cp t " +
            "LEFT JOIN cp_info t1 ON t.id=t1.cp_id " +
            "LEFT JOIN (select cp_id,SUM(actual_amount)total,SUM(fee)fee from `order` where state=3 GROUP BY cp_id)t3 ON t.id=t3.cp_id " +
            "LEFT JOIN (select cp_id,SUM(amount)drawAmount FROM cp_withdraw_tradlog where state in(0,2) GROUP BY cp_id)t4 ON t.id=t4.cp_id " +
            "LEFT JOIN (select cp_id,SUM(actual_amount)disTotal from `order` where state=4 GROUP BY cp_id)t5 ON t.id=t5.cp_id " +
            "where t.type=1")
    @Results({
            @Result(column = "total", property = "total", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.BIGINT),
            @Result(column = "widthdraw_amount", property = "widthdrawAmount", jdbcType = JdbcType.BIGINT),
            @Result(column = "balance_disabled", property = "balanceDisabled", jdbcType = JdbcType.BIGINT)
    })
    CpAccount CpAccountTj();

    @Select("SELECT IFNULL(SUM(t3.total),0) + IFNULL(SUM(t5.disTotal),0)total,IFNULL(SUM(t3.fee),0)fee,IFNULL(SUM(t4.drawAmount),0)widthdraw_amount,IFNULL(sum(t5.disTotal),0)balance_disabled from cp t " +
            " LEFT JOIN cp_info t1 ON t.id=t1.cp_id " +
            " LEFT JOIN (select cp_id,SUM(actual_amount)total,SUM(fee)fee from `order` where state=3 GROUP BY cp_id)t3 ON t.id=t3.cp_id  " +
            " LEFT JOIN (select cp_id,SUM(amount)drawAmount FROM cp_withdraw_tradlog where state in(0,2) GROUP BY cp_id)t4 ON t.id=t4.cp_id " +
            " LEFT JOIN (select cp_id,SUM(actual_amount)disTotal from `order` where state=4 GROUP BY cp_id)t5 ON t.id=t5.cp_id  " +
            "where t.type=1 and t.agent_id=#{agentId}")
    @Results({
            @Result(column = "total", property = "total", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee", property = "fee", jdbcType = JdbcType.BIGINT),
            @Result(column = "widthdraw_amount", property = "widthdrawAmount", jdbcType = JdbcType.BIGINT),
            @Result(column = "balance_disabled", property = "balanceDisabled", jdbcType = JdbcType.BIGINT)
    })
    CpAccount CpAccountTjAgent(Long agentId);


    @Select({
            "select * from `cp` order by id desc limit #{offset}, #{size}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "app_id", property = "appId", jdbcType = JdbcType.CHAR),
            @Result(column = "secret_id", property = "secretId", jdbcType = JdbcType.CHAR),
            @Result(column = "agent_id", property = "agentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee_default_recharge", property = "feeDefaultRecharge", jdbcType = JdbcType.REAL),
            @Result(column = "cost_withdraw", property = "costWithdraw", jdbcType = JdbcType.REAL),
            @Result(column = "fee_withdraw_advince", property = "feeWithdrawAdvince", jdbcType = JdbcType.REAL),
            @Result(column="withdraw_password", property="withdrawPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column = "notify_url", property = "notifyUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ip", property = "ip", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bind_mobile", property = "bindMobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "account_state", property = "accountState", jdbcType = JdbcType.TINYINT),
            @Result(column = "contract_state", property = "contractState", jdbcType = JdbcType.TINYINT)
    })
    List<Cp> selectAll(@Param("offset") int offset, @Param("size") int size);

    @Select({"select count(id) from `cp`"})
    long countAll();

    @Select({
            "select * from `cp` where `type`=#{type} order by id desc limit #{offset}, #{size}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "app_id", property = "appId", jdbcType = JdbcType.CHAR),
            @Result(column = "secret_id", property = "secretId", jdbcType = JdbcType.CHAR),
            @Result(column = "agent_id", property = "agentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee_default_recharge", property = "feeDefaultRecharge", jdbcType = JdbcType.REAL),
            @Result(column = "cost_withdraw", property = "costWithdraw", jdbcType = JdbcType.REAL),
            @Result(column = "fee_withdraw_advince", property = "feeWithdrawAdvince", jdbcType = JdbcType.REAL),
            @Result(column="withdraw_password", property="withdrawPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column = "notify_url", property = "notifyUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ip", property = "ip", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bind_mobile", property = "bindMobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "account_state", property = "accountState", jdbcType = JdbcType.TINYINT),
            @Result(column = "contract_state", property = "contractState", jdbcType = JdbcType.TINYINT)
    })
    List<Cp> selectByType(@Param("type") byte type, @Param("offset") int offset, @Param("size") int size);

    @Select({"select count(id) from `cp` where `type`=#{type}"})
    long countByType(@Param("type") byte type);

    @Select({
            "select * from `cp` where agent_id=#{agentId} order by id desc limit #{offset}, #{size}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "app_id", property = "appId", jdbcType = JdbcType.CHAR),
            @Result(column = "secret_id", property = "secretId", jdbcType = JdbcType.CHAR),
            @Result(column = "agent_id", property = "agentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee_default_recharge", property = "feeDefaultRecharge", jdbcType = JdbcType.REAL),
            @Result(column = "cost_withdraw", property = "costWithdraw", jdbcType = JdbcType.REAL),
            @Result(column = "fee_withdraw_advince", property = "feeWithdrawAdvince", jdbcType = JdbcType.REAL),
            @Result(column="withdraw_password", property="withdrawPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column = "notify_url", property = "notifyUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ip", property = "ip", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bind_mobile", property = "bindMobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "account_state", property = "accountState", jdbcType = JdbcType.TINYINT),
            @Result(column = "contract_state", property = "contractState", jdbcType = JdbcType.TINYINT)
    })
    List<Cp> selectByAgentId(@Param("agentId") long agentId, @Param("offset") int offset, @Param("size") int size);

    @Select({"select count(id) from `cp` where agent_id=#{agentId}"})
    long countByAgentId(@Param("agentId") long agentId);

    @Select({"select * from `cp` where app_id=#{app_id}"})
    Cp selectByAppId(@Param("app_id") String appId);

    @Select({"select * from cp where agent_id= #{agentId} and account_state= #{accountState} order by id desc"})
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "app_id", property = "appId", jdbcType = JdbcType.CHAR),
            @Result(column = "secret_id", property = "secretId", jdbcType = JdbcType.CHAR),
            @Result(column = "agent_id", property = "agentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee_default_recharge", property = "feeDefaultRecharge", jdbcType = JdbcType.REAL),
            @Result(column = "cost_withdraw", property = "costWithdraw", jdbcType = JdbcType.REAL),
            @Result(column = "fee_withdraw_advince", property = "feeWithdrawAdvince", jdbcType = JdbcType.REAL),
            @Result(column="withdraw_password", property="withdrawPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column = "notify_url", property = "notifyUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ip", property = "ip", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bind_mobile", property = "bindMobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "account_state", property = "accountState", jdbcType = JdbcType.TINYINT),
            @Result(column = "contract_state", property = "contractState", jdbcType = JdbcType.TINYINT)
    })
    List<Cp> findByAgentIdAndAccountState(@Param("agentId") Long agentId, @Param("accountState") Byte accountState);

    @Select({
            "select * from `cp` where account_state= 2 order by agent_id asc, id asc"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "app_id", property = "appId", jdbcType = JdbcType.CHAR),
            @Result(column = "secret_id", property = "secretId", jdbcType = JdbcType.CHAR),
            @Result(column = "agent_id", property = "agentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "fee_default_recharge", property = "feeDefaultRecharge", jdbcType = JdbcType.REAL),
            @Result(column = "cost_withdraw", property = "costWithdraw", jdbcType = JdbcType.REAL),
            @Result(column = "fee_withdraw_advince", property = "feeWithdrawAdvince", jdbcType = JdbcType.REAL),
            @Result(column="withdraw_password", property="withdrawPassword", jdbcType=JdbcType.VARCHAR),
            @Result(column = "notify_url", property = "notifyUrl", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ip", property = "ip", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bind_mobile", property = "bindMobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "account_state", property = "accountState", jdbcType = JdbcType.TINYINT),
            @Result(column = "contract_state", property = "contractState", jdbcType = JdbcType.TINYINT)
    })
    List<Cp> listAll();

    @Select({
            "select c.id as id,c.agent_id as agentId,ci.name as name from `cp` c inner join cp_info ci on c.id= ci.cp_id where c.account_state= 2 and c.type=1 " +
                    "and agent_id= #{agentId} order by c.agent_id asc, c.id asc"
    })

    List<CpZhiding> listAllZd(@Param("agentId") int agentId);


}